//
//  ZHConsultMainFineViewController.m
//  Doctor
//
//  Created by liuzhishan on 16/1/18.
//  Copyright © 2016年 shanshan. All rights reserved.
//



#define  ITEMCOUNT 4
#import "ZHConsultMainFineViewController.h"

#import "ZHConsultMessageViewController.h"
#import "ZHConsultPastCaseViewController.h"
#import "ZHConsultPrivilegeViewController.h"
#import "ZHConsultProjectViewController.h"
#import "ZHWeChatTabBar.h"



@interface ZHConsultMainFineViewController ()<clickTabBarIndex,UIScrollViewDelegate>

{
    int judge[ITEMCOUNT];
}


@property (nonatomic,strong) ZHWeChatTabBar *weChatTabBar;
@property (nonatomic,strong) NSArray *titleArray;
@property (nonatomic,strong) NSArray *imageNormalArray;
@property (nonatomic,strong) NSArray *imageHighArray;
@property (nonatomic,strong) NSArray *controllerArray;
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,assign) CGFloat currentOffset;


@end

@implementation ZHConsultMainFineViewController

#pragma mark 实现代理 返回Index
-(void)getTabBarIndex:(NSInteger)index
{
    
     [self addMyFirstVC:index];
    
     self.scrollView.contentOffset = CGPointMake(index * WIDTH, 0);
    self.currentOffset = self.scrollView.contentOffset.x;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    [self setInitJudgeData];
    [self addMyChildController];
    [self addMyScrollView];
    [self addMyFirstVC:0];
    
    
}

-(void)addMyFirstVC:(NSInteger)index
{
    
    if (judge[index] == 0) {
        
        judge[index] = 1;
    
    UIViewController *firstVC = self.controllerArray[index];
    firstVC.view.frame = CGRectMake(WIDTH * index, 0, WIDTH, HEIGHT);
    
    [self.scrollView addSubview:firstVC.view];
    [self addChildViewController:firstVC];
        
    }

}

#pragma mark 初始化数组数据

-(void)setInitJudgeData
{
    
    self.currentOffset = 0;
    
    for ( int i = 0 ;  i< ITEMCOUNT ;  i++) {
        
        judge[i] = 0;
        
    }
   
    
}

#pragma mark 添加scrollView
-(void)addMyScrollView
{
    self.scrollView = [[UIScrollView alloc]init];
    
    self.scrollView.frame = self.view.bounds;
    
    [self.view addSubview:self.scrollView];
    
    self.scrollView.contentSize = CGSizeMake(WIDTH * 4, HEIGHT);
    self.scrollView.delegate = self;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.bounces = NO;
    self.scrollView.pagingEnabled = YES;
    
    
    ZHWeChatTabBar *weChatTabBar = [[ZHWeChatTabBar alloc]init];
    self.weChatTabBar = weChatTabBar;
    weChatTabBar.tabBarDele = self;
    weChatTabBar.backgroundColor = [UIColor purpleColor];
    
    weChatTabBar.titleArray = self.titleArray;
    weChatTabBar.imageNormalArray = self.imageNormalArray;
    weChatTabBar.imageHighArray = self.imageHighArray;

    [self.view addSubview:weChatTabBar];
    
    [weChatTabBar mas_makeConstraints:^(MASConstraintMaker *make) {

        CGFloat  leftPro = (1.0 - TABWIGHTPRO)/2.0;
        make.bottom.equalTo(@0);
        make.left.equalTo(@(WIDTH * leftPro));
        make.right.equalTo(@(-WIDTH *leftPro));
        make.height.equalTo(@64);
        
    }];
    
   
}


#pragma mark 添加VC的信息
-(void)addMyChildController
{
    
    self.controllerArray = [NSMutableArray array];
    
     ZHConsultMessageViewController *news = [[ZHConsultMessageViewController alloc]init];
     ZHConsultPastCaseViewController *read = [[ZHConsultPastCaseViewController alloc]init];
     ZHConsultPrivilegeViewController *listen = [[ZHConsultPrivilegeViewController alloc]init];
     ZHConsultProjectViewController *me = [[ZHConsultProjectViewController alloc]init];
    
    self.controllerArray = @[news,read,listen,me];
    
    self.titleArray = @[@"微信",@"通讯录",@"发现",@"我"];
    
    self.imageNormalArray = @[@"tabbar_mainframe",@"tabbar_contacts",@"tabbar_discover",@"tabbar_me"];
    
    self.imageHighArray = @[@"tabbar_mainframeHL",@"tabbar_contactsHL",@"tabbar_discoverHL",@"tabbar_meHL"];
    
    
}


#pragma mark scrollView滚动





-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger currentIndex  = 0;
    NSInteger nextIndex = 0;
    CGFloat   offsetProportion = 0;
    
    if (scrollView.contentOffset.x > self.currentOffset) {
       
        currentIndex = scrollView.contentOffset.x / WIDTH;
        nextIndex = currentIndex  +  1;
 
        NSLog(@"右滑－－－－－－－－－");
        
    }
    
   else  if (scrollView.contentOffset.x < self.currentOffset) {
       
       currentIndex = scrollView.contentOffset.x / WIDTH;
      // currentIndex++;
       
      // 不会出现的（出现也是一瞬间） 正好在整数
       if (!(scrollView.contentOffset.x - currentIndex * WIDTH) <= 0.001) {
          
           currentIndex ++;
       }
       
       else
       {
           
          NSLog(@"555555");
           
       }
       
       
       nextIndex = currentIndex  -  1;
       
        NSLog(@"左滑－－－－－－－－－");
       

        
    }
    
    else
    {
        NSLog(@"夏季吧 滑");
    }
    
    
    if(!(nextIndex < 0  || nextIndex >= ITEMCOUNT))
    {
        [self addMyFirstVC:nextIndex]; 
    }
    
    NSLog(@"%ld ---- %ld",currentIndex,nextIndex);
    
    
    offsetProportion = ABS(scrollView.contentOffset.x - (WIDTH * currentIndex)) / WIDTH;
    
    self.currentOffset = scrollView.contentOffset.x;
    
    [self.weChatTabBar  sliderOffset:offsetProportion andCurrentIndex:currentIndex andNextIndex:nextIndex];
    
    
    
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
